<?php
/**
 * created by zzy
 * date: 2018/1/26 14:04
 */
namespace App\Eloquent\Ygt;

class Warehouse extends DbEloquent{

    protected $table    = 'ygt_warehouse';
    public static $tableFieldList   = [
        ['name'=>'id','title'=>'','value'=>0,'is_must'=>0],
        ['name'=>'company_id','title'=>'登录用户异常','value'=>0,'is_must'=>1],
        ['name'=>'user_id','title'=>'登录用户异常','value'=>0,'is_must'=>1],
        ['name'=>'order_id','title'=>'工单','value'=>0,'is_must'=>1],
        ['name'=>'price','title'=>'单价','value'=>0.00,'is_must'=>1],
        ['name'=>'customer_id','title'=>'客户','value'=>0,'is_must'=>1],
        ['name'=>'all_number','title'=>'入库数量','value'=>0,'is_must'=>1],
        ['name'=>'now_number','title'=>'入库数量','value'=>0,'is_must'=>1],
    ];
    public static function edit($data,$pk='')
    {
        $id                 = isset($data['id']) ? intval($data['id']) : 0;
        if($id>0){
            $where          = ['id'=>$id];
            $result         = self::updateOneData($where,$data,$pk);
        } else {
            $result         = self::insertOneData($data,$pk);
        }
        return $result;
    }
    //由入库单id取对应的工单信息
    public static function getOrderListByCustomerId($customerId,$limit='',$offset='')
    {
        $where              = [
            'ygt_warehouse.customer_id'=>$customerId,'ygt_warehouse.now_number'=>['>',0]
        ];
        $result             = self::getOrderList($where,$limit,$offset);
        return $result;

    }
    public static function getOrderListByWarehouseIdArr($warehouseIdArr)
    {
        $where              = [
            'ygt_warehouse.id'=>['in',$warehouseIdArr]
        ];
        $result             = self::getOrderList($where);
        return $result;
    }
    public static function getOrderList($whereArr,$limit='',$offset='')
    {
        $join               = [
            ['table'=>'ygt_order', 'field_l'=>'ygt_order.id', 'field_c'=>'=', 'field_r'=>'ygt_warehouse.order_id'],
        ];
        $column             = 'ygt_warehouse.id as warehouse_id,ygt_warehouse.order_id,ygt_warehouse.all_number,';
        $column             .= 'ygt_warehouse.now_number,ygt_warehouse.bill_number,ygt_warehouse.price,';
        $column             .= 'ygt_order.product_name,ygt_order.finished_specification,ygt_order.finished_date,';
        $column             .= 'ygt_order.order_type,ygt_order.field_name_23,ygt_order.grammage,';
        $column             .= 'ygt_order.production_case_diagram,ygt_order.customer_name';
        $orderby            = ['ygt_warehouse.id','desc'];
        $groupby            = '';
        $collection         = self::getList($whereArr,$column,$limit,$offset,$orderby,$groupby,$join);
        $result             = $collection->toArray();
        return $result;
    }
     //取列表
    public static function getList( $where, $fileds='', $limit='', $offset='', $orderby='', $groupby='', $join='' ){

        $collection         = self::getData( $where, $fileds, $limit, $offset, $orderby, $groupby, $join );
        $lists              = $collection->toArray();
        foreach($lists as $key=>$val)
        {
            if(array_key_exists('img_path',$val))
            {
                $collection[$key]->img_path = \App\Engine\Func::getImgUrlHttp($val['img_path']);
            }
        }
        return $collection;
    }
}
